using System;
using System.Data;
using System.Windows.Forms;
using TX.Data.DSTableAdapters;

namespace TX
{
    public partial class Product : Base2
    {
        public Product()
        {
            InitializeComponent();

            tableAdapterManager.ProductTableAdapter = new ProductTableAdapter();
            tableAdapterManager.ProductDescriptionTableAdapter = new ProductDescriptionTableAdapter();
            tableAdapterManager.ProductDocumentTableAdapter = new ProductDocumentTableAdapter();
            tableAdapterManager.ProductListPriceHistoryTableAdapter = new ProductListPriceHistoryTableAdapter();
            tableAdapterManager.ProductPhotoTableAdapter = new ProductPhotoTableAdapter();
            tableAdapterManager.ProductVendorTableAdapter = new ProductVendorTableAdapter();
            tableAdapterManager.FeatureAksesuarTableAdapter = new FeatureAksesuarTableAdapter();
        }


        //public override void Kaydet()
        //{
        //    Save();
        //}

        private void Product_Load(object sender, EventArgs e)
        {
            try
            {
                gridView1.BeginUpdate();
                bs1 = new BindingSource(ds, "Product");

                bindingNavigator1.BindingSource = bs1;

                tableAdapterManager.ProductTableAdapter.Fill(ds.Product);

                bs1.CurrentChanged += new EventHandler(bs1_CurrentChanged);

                //BelgeTipiIdLookUpEdit.Properties.Assign(riBelgeTipi);
                //VendorIdLookUpEdit.Properties.Assign(riVendor);
                //AlisSekliIdLookUpEdit.Properties.Assign(riAlisSekli);
                //CarrierIdLookUpEdit.Properties.Assign(riCarrier);
                //DeliveryTermIdLookUpEdit.Properties.Assign(riDeliveryTerm);
                //CurrencyCodeLookUpEdit.Properties.Assign(riCurrency);
                //ExpensePercentageIdLookUpEdit.Properties.Assign(riExpensePercentage);
                //PaymentTermIdLookUpEdit.Properties.Assign(riPaymentTerm);
                //OwnerIdLookUpEdit.Properties.Assign(riCustomer);

                //dataLayoutControl1.OptionsView.HighlightFocusedItem = true;
                gridControl1.DataSource = this.bs1;
                InitGridView(gridView1, true);
                gridView1.BestFitColumns();

                //this.gridView1.OptionsBehavior.Editable = false;
                //this.gridView1.OptionsView.ColumnAutoWidth = false;
                //this.gridView1.OptionsView.EnableAppearanceOddRow = true;
                //this.gridView1.OptionsView.ShowAutoFilterRow = true;
                //gridView1.UnselectRow(1);
                //this.gridView1.OptionsSelection.EnableAppearanceFocusedRow = false;
                gridView1.EndUpdate();

                bs2 = new BindingSource(bs1, "FK_ProductProductPhoto_Product_ProductId");
                bs3 = new BindingSource(bs1, "FK_ProductVendor_Product_ProductId");
                bs4 = new BindingSource(bs1, "FK_ProductCostHistory_Product_ProductId");
                bs5 = new BindingSource(bs1, "FK_FeatureAksesuar_Stok");

                //gridControl2.DataSource = bs2;  // ds.ProductPhoto;
                gridControl3.DataSource = bs3;  // ds.ProductVendor;
                gridControl4.DataSource = bs4;  // ds.ProductCostHistory;

                //gridView3.OptionsView.RowAutoHeight = true;
                //InitLayoutView(layoutView1);
                //InitGridView(gridView3);
                InitGridView(gridView4);
                InitGridView(gridView5);

                DLCBind("dataLayoutControl1", bs2);
                DLCBind("dataLayoutControl2", bs5);
                UnitMeasureIdLookUpEdit.Properties.Assign(riUnitMeasure);
                
                CurrentChanged();

            }
            catch (Exception Ex)
            {
                //throw;
            }

        }

        int productId;
        void bs1_CurrentChanged(object sender, EventArgs e)
        {
            if (!saving)
            {
                Save();
                CurrentChanged();
            }
        }

        void CurrentChanged()
        {
            if (bs1.Current != null)
            {
                productId = int.Parse(((DataRowView)bs1.Current).Row["ProductId"].ToString());
                this.Text = "Product : " + ((DataRowView)bs1.Current).Row["StokAdi"].ToString();
            }

            if(xtraTabControl1.SelectedTabPageIndex > 0)
                RefreshData();
        }


        private void RefreshData()
        {
            ds.EnforceConstraints = false;
            //productPhotoTableAdapter.ClearBeforeFill = true;
            //productPhotoTableAdapter.FillByProductId(ds.ProductPhoto, productId);

            //productVendorTableAdapter.ClearBeforeFill = true;
            //productVendorTableAdapter.FillByProductId(ds.ProductVendor, productId);
            //gridView4.BestFitColumns();

            //featureAksesuarTableAdapter.ClearBeforeFill = true;
            //featureAksesuarTableAdapter.FillByProductId(ds.FeatureAksesuar, productId);
        }



        public override void Save2()
        {
            //Submit inserts and updates top-down, then deletes bottom-up
            //Since there is only Orders and Order Details

            //DataRow[] rowsToSubmit;
            //rowsToSubmit = ds.Product.Select("", "", Added);
            //if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.productTableAdapter.Update(rowsToSubmit);

            //rowsToSubmit = ds.ProductPhoto.Select("", "", Added);
            //if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.productPhotoTableAdapter.Update(rowsToSubmit);

            //rowsToSubmit = ds.ProductVendor.Select("", "", Added);
            //if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.productVendorTableAdapter.Update(rowsToSubmit);

            ////Submit deleted orders
            //rowsToSubmit = ds.ProductVendor.Select("", "", Deleted);
            //if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.productVendorTableAdapter.Update(rowsToSubmit);

            //rowsToSubmit = ds.ProductPhoto.Select("", "", Deleted);
            //if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.productPhotoTableAdapter.Update(rowsToSubmit);

            //rowsToSubmit = ds.Product.Select("", "", Deleted);
            //if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.productTableAdapter.Update(rowsToSubmit);

            ds.AcceptChanges();
        }

        private void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            if (!saving)
            {
                Save();
                CurrentChanged();
            }
        }



        //private void gridView1_DoubleClick(object sender, EventArgs e)
        //{
        //    //            EFatura_Yeni efatura = new EFatura_Yeni(((TX.Data.DSx1.FaturaRow)((System.Data.DataRowView)gridView1.GetFocusedRow()).Row).FaturaId);
        //    EFatura efatura = new EFatura(int.Parse(((System.Data.DataRowView)gridView1.GetFocusedRow()).Row["FaturaId"].ToString()));
        //    MdiShow(efatura, this.ParentForm);

        //}
    }
}